<template>
  <div class="home-container">
    <div class="hader">
      <van-nav-bar class="page-nav-bar" fixed>
        <van-button
          class="search-btn"
          slot="title"
          type="info"
          size="small"
          round
          icon="search"
          to='/search'
          >搜索</van-button
        >
      </van-nav-bar>
    </div>
    <van-tabs v-model="active" animated swipeable class="list-cont">
      <van-tab
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name"
      >
        <articleList :channel="channel" />
      </van-tab>
      <div class="hanbaowz" slot="nav-right"></div>
      <div slot="nav-right" class="hanbao" @click="isShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <van-popup
      v-model="isShow"
      position="top"
      closeable
      :style="{ height: '100%' }"
      close-icon-position="top-left"
      ><channel-edit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/users'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIdex',
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  data () {
    return {
      // 用户频道组件选择属性
      active: '',
      // 用户频道内容属性
      channels: [],
      isShow: false
    }
  },
  created () {
    // 获取用户频道信息
    this.loadChannels()
  },
  methods: {
    // 获取用户频道信息请求
    async loadChannels () {
      try {
        // const { data } = await getUserChannels()
        // this.channels = data.data.channels
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          this.channels = data.data.channels
        } else {
          const loadChannels = getItem('TOUTIAO_CHANNELS')
          if (loadChannels) {
            channels = loadChannels
          } else {
            const { data } = await getUserChannels()
            this.channels = data.data.channels
          }
        }
        this.channels = channels
        console.log('用户频道', this.channels)
      } catch (err) {
        this.$toast('获取用户频道失败')
      }
    },
    onUpdateActive (index, isShow = true) {
      console.log(index)
      this.active = index
      this.isShow = isShow
    }
    // async onLoad () {
    //   try {
    //     const { data } = await getArticle({
    //       channel_id: this.channel.id,
    //       timestamp: Date.now()
    //     })
    //     console.log(data)
    //   } catch (err) {
    //     console.log('获取用户频道列表信息失败', err, this.channel_id, Date.now())
    //   }
    // }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  margin-top: 180px;
  .page-nav-bar {
    /deep/ .van-nav-bar__title {
      max-width: 100%;
    }
    .search-btn {
      width: 555px;
      height: 64px;
      background-color: #5babfb;
      font-size: 28px;
      .van-icon {
        font-size: 32px;
      }
    }
  }
  .van-tabs {
    /deep/ .van-tab {
      min-width: 180px;
      color: #777777;
    }
    /deep/ .van-tab--active {
      color: #333333;
    }
    /deep/ .van-tabs__line {
      width: 32px;
      background-color: #2089ff;
    }
    .hanbaowz {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hanbao {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.7;
      i {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url('~@/assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
  }
  .list-cont {
    margin-bottom: 100px;
    /deep/ .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
  }
}
</style>
